package com.lin.nio.channel.block;

import com.lin.nio.utils.ByteBufferUtil;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class TCPClient {
    public static void main(String[] args) {
        try {
            startClient();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void startClient() throws IOException {
        /// 创建一个SocketChannel
        SocketChannel sc = SocketChannel.open();
        sc.connect(new InetSocketAddress("localhost", 1122));
        System.out.println("waiting...");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sc.write(Charset.defaultCharset().encode("hello server"));

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ByteBuffer dst = ByteBuffer.allocate(16);
        sc.read(dst);
        System.out.println("收到消息：");
        ByteBufferUtil.debugAll(dst);

        sc.close();
    }

}
